//
//  ZDBAddAddreassViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/19.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBAddAddreassViewController.h"
#import "CGXPickerView.h"
#import "NSString+handle.h"

@interface ZDBAddAddreassViewController ()
@property(nonatomic,strong)NSArray * seletAdressArry;
@property(nonatomic,strong)NSArray * seletAdressRowArry;
@property(nonatomic,strong)NSString * seletAdressStr;
@property(nonatomic,strong)NSString * adress1;
@property(nonatomic,strong)NSString * adress2;
@property(nonatomic,strong)NSString * adress3;
@end

@implementation ZDBAddAddreassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.seletAdressRowArry =@[@0, @0,@0];
    self.seletAdressArry =@[];
    [self initCustom];
}
- (void)initTextFieldWithTextField:(UITextField *)TextField
{
    TextField.font = AutoBoldFont(15);
    
    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(TextField, ivar);
    placeholderLabel.textColor = ColorStr(@"#999999");
    placeholderLabel.font = AutoFont(13);
    TextField.tintColor = MAINCOLOR;
    TextField.textColor = MAINTITLECOLOR;
    
}
- (void)initCustom
{
    
    if (self.isEdit==YES) {
        self.nameTextField.text = self.adsModel.name;
        self.phoneTextField.text = [NSString stringWithFormat:@"%ld",[self.adsModel.tel integerValue]];
        self.addressTextView.text = self.adsModel.way;
        self.adress1 = self.adsModel.province;
        self.adress2 = self.adsModel.city;
        self.adress3 = self.adsModel.county;
        self.seletAdressStr = [NSString stringWithFormat:@"%@%@%@", self.adress1, self.adress2, self.adress3];
        self.areaValueLab.text = self.seletAdressStr;
        
        if ([self.adsModel.type integerValue]==1) {
            self.defaultButton.selected = YES;
        }else{
            self.defaultButton.selected = NO;
        }
        
    }
    KViewRadius(_saveBtn, 4);
    [self initTextFieldWithTextField:_nameTextField];
    [self initTextFieldWithTextField:_phoneTextField];
    WeakSelf
    [_defaultButton add_BtnClickHandler:^(NSInteger tag) { //设置默认地址
        [weakSelf.view endEditing:YES];
        weakSelf.defaultButton.selected =! weakSelf.defaultButton.selected;
        [weakSelf addDefaultWithAddreass:weakSelf.defaultButton.selected];
    }];
    [_chooseAreaBtn add_BtnClickHandler:^(NSInteger tag) { //选择地区
        [weakSelf.view endEditing:YES];
        [weakSelf show_AdressView];
    }];
    [_saveBtn add_BtnClickHandler:^(NSInteger tag) { //保存地址

        [weakSelf.view endEditing:YES];
        [weakSelf saveAdressRequest];
    }];
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(event:)];
    [tapGesture setNumberOfTapsRequired:1];
    [_bgView addGestureRecognizer:tapGesture];
    
}

-(void)show_AdressView{
    
    CGXPickerViewManager *manager = [[CGXPickerViewManager alloc]init];
    manager.titleLabelColor= kRGBColor(91, 91, 91); //kRGBColor(44, 143, 219);
    manager.leftBtnBGColor= [UIColor whiteColor];
    manager.leftBtnTitle= @"";
    manager.leftBtnTitleSize=0;
    manager.rightBtnBGColor= [UIColor whiteColor];
    manager.leftBtnTitleColor= kRGBColor(161, 161, 161);
    manager.rightBtnTitleColor=kRGBColor(44, 143, 219);
    manager.leftBtnborderColor= [UIColor whiteColor];
    manager.rightBtnborderColor= [UIColor whiteColor];
    WEAKSELF
    [CGXPickerView showAddressPickerWithTitle:@"请选择省市区" DefaultSelected:self.seletAdressRowArry IsAutoSelect:YES Manager:manager ResultBlock:^(NSArray *selectAddressArr, NSArray *selectAddressRow) {
        STRONGSELFFor(weakSelf);
        NSLog(@"%@-%@",selectAddressArr,selectAddressRow);
        strongSelf.seletAdressRowArry = selectAddressArr;
        strongSelf.seletAdressArry = selectAddressArr;
        strongSelf.adress1 = selectAddressArr.firstObject;
        strongSelf.adress2 = selectAddressArr[1];
        strongSelf.adress3 = selectAddressArr.lastObject;
        strongSelf.seletAdressStr = [NSString stringWithFormat:@"%@%@%@", strongSelf.adress1, strongSelf.adress2, strongSelf.adress3];
        strongSelf.areaValueLab.text =strongSelf.seletAdressStr;
    }];
    
}

-(void)saveAdressRequest{

    
    if (kStringIsEmpty(self.nameTextField.text)) {
        [JMNotifyView showNotify:@"请输入收货人姓名" isSuccessful:NO];
        return;
    }
    
    if (kStringIsEmpty(self.phoneTextField.text)) {
           [JMNotifyView showNotify:@"请输入收货人电话" isSuccessful:NO];
           return;
       }
    
    if ([NSString isMobileNumChack:self.phoneTextField.text]==NO) {
        [JMNotifyView showNotify:@"请输入正确的手机号" isSuccessful:NO];
                  return;
    }
    if (kStringIsEmpty(self.phoneTextField.text)) {
           [JMNotifyView showNotify:@"请输入收货人电话" isSuccessful:NO];
           return;
       }
    if (kStringIsEmpty(self.seletAdressStr)) {
        [JMNotifyView showNotify:@"请选择收货省市区县" isSuccessful:NO];
        return;
    }
    if (kStringIsEmpty(self.addressTextView.text)) {
          [JMNotifyView showNotify:@"请输入收货详细地址" isSuccessful:NO];
          return;
      }
    
    NSString  * inurl = @"";
    NSMutableDictionary * dicc = [NSMutableDictionary dictionary];
    //userAddress_addUserAddress
    if (self.isEdit==YES) {
        inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"updateUserAddress"];
        [dicc setValue:self.adsModel.id forKey:@"id"];
        [dicc setValue:[NSString stringWithFormat:@"%@%@",self.seletAdressStr,self.addressTextView.text] forKey:@"address"];
             [dicc setValue:self.nameTextField.text forKey:@"name"];
             [dicc setValue:self.phoneTextField.text forKey:@"tel"];
             if (self.defaultButton.selected ==YES) {
               [dicc setValue:@(1) forKey:@"type"];
             }else{
               [dicc setValue:@(2) forKey:@"type"];
             }
             [dicc setValue:self.adress1 forKey:@"province"];
             [dicc setValue:self.adress2 forKey:@"city"];
             [dicc setValue:self.adress3 forKey:@"county"];
             [dicc setValue:self.addressTextView.text forKey:@"way"];
    }else{
        inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"userAddress_addUserAddress"];
        [dicc setValue:[NSString stringWithFormat:@"%@%@",self.seletAdressStr,self.addressTextView.text] forKey:@"address"];
        [dicc setValue:self.nameTextField.text forKey:@"name"];
        [dicc setValue:self.phoneTextField.text forKey:@"tel"];
        if (self.defaultButton.selected ==YES) {
          [dicc setValue:@(1) forKey:@"type"];
        }else{
          [dicc setValue:@(2) forKey:@"type"];
        }
        [dicc setValue:self.adress1 forKey:@"province"];
        [dicc setValue:self.adress2 forKey:@"city"];
        [dicc setValue:self.adress3 forKey:@"county"];
        [dicc setValue:self.addressTextView.text forKey:@"way"];
    }

       WEAKSELF
       [[SmileHttpTool sharedInstance] POST :inurl parameters:dicc origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
         STRONGSELFFor(weakSelf)
           
           if (statusCode==0) {
           NSLog(@"userAddress_addUserAddress----%@",responseObject);
             [JMNotifyView showNotify:@"保存地址成功" isSuccessful:YES];
               if (strongSelf.actionBtnClickBlock) {
                   strongSelf.actionBtnClickBlock(YES);
               }
               [strongSelf.navigationController popViewControllerAnimated:YES];
           }else{
                [JMNotifyView showNotify:message isSuccessful:NO];
           }
             
           // [strongSelf stopLoadingAnimation];
           
       } failure:^(id responseObject, NSError *error) {
           NSLog(@"error----%@",error);
           // STRONGSELFFor(weakSelf)
            
           [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
           // [self stopLoadingAnimation];
       }];
    
    
}
-(void)event:(UITapGestureRecognizer *)gesture
{
    _defaultButton.selected =!_defaultButton.selected;
    [self addDefaultWithAddreass:_defaultButton.selected];
}
#pragma 是否添加为默认地址
- (void)addDefaultWithAddreass:(BOOL)isDefault
{
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
